﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;

public class UGUScrollViewTools : Editor
{

    [MenuItem("Plateface/ScrollView/CreatePosition #&Q")]
    public static void CreatePosition()
    {
        if (Selection.gameObjects.Length > 0)
        {
            Debug.Log("获取的对象" + Selection.gameObjects[0].name);

            RectTransform selectionRect = (RectTransform)Selection.gameObjects[0].transform.GetComponent<RectTransform>();

            GameObject obj = new GameObject();
            obj.AddComponent(typeof(RectTransform));
            obj.transform.SetParent(Selection.gameObjects[0].transform.parent);
            obj.name = "Target";
            obj.GetComponent<RectTransform>().anchorMin = new Vector2(0f, 0.5f);
            obj.GetComponent<RectTransform>().anchorMax = new Vector2(0f, 0.5f);
            obj.GetComponent<RectTransform>().anchoredPosition = selectionRect.anchoredPosition;


            EditorUtility.SetDirty(obj);
            EditorUtility.SetDirty(Selection.gameObjects[0]);
        }
    }


    [MenuItem("Plateface/ScrollView/CreatePositionList")]
    public static void CreatePositionList()
    {
        if (Selection.gameObjects.Length > 0)
        {
    
            GameObject obj;
            int index = 0;
            foreach (var i in Selection.gameObjects)
            {
                RectTransform selectionRect = (RectTransform)i.transform.GetComponent<RectTransform>();
                obj = new GameObject();
                obj.AddComponent(typeof(RectTransform));
                obj.transform.SetParent(Selection.gameObjects[0].transform.parent);
                obj.name = "Target" + index;
                obj.GetComponent<RectTransform>().anchorMin = new Vector2(0f, 0.5f);
                obj.GetComponent<RectTransform>().anchorMax = new Vector2(0f, 0.5f);
                obj.GetComponent<RectTransform>().anchoredPosition =
                    new Vector2(selectionRect.anchoredPosition.x - selectionRect.sizeDelta.x / 2, selectionRect.anchoredPosition.y);

                index++;

                EditorUtility.SetDirty(obj);
                EditorUtility.SetDirty(Selection.gameObjects[0]);
            }
        }
    }

    [MenuItem("Plateface/ScrollView/CopyGameObjectX2 #M")]
    public static void CopyXx2()
    {
        GameObject obj;
        int index = 0;

        if (Selection.gameObjects.Length == 1)
        {
            string name = Selection.gameObjects[0].name;
            if ((int)name[name.Length - 2] >= 48 && (int)name[name.Length - 2] <= 57)
            {
                index = int.Parse(name.Substring(name.Length - 2)) + 1;
            }
            else
            {
                if ((int)name[name.Length - 1] >= 48 && (int)name[name.Length - 1] <= 57)
                {
                    index = name[name.Length - 1] + 1 - 48;
                }
            }
            
            RectTransform selectionRect = (RectTransform)Selection.gameObjects[0].transform.GetComponent<RectTransform>();
            obj = new GameObject();
            obj.AddComponent(typeof(RectTransform));
            obj.transform.SetParent(Selection.gameObjects[0].transform.parent);
            obj.name = "Target" + index;
            obj.GetComponent<RectTransform>().anchorMin = new Vector2(0f, 0.5f);
            obj.GetComponent<RectTransform>().anchorMax = new Vector2(0f, 0.5f);
            obj.GetComponent<RectTransform>().anchoredPosition =
                new Vector2(selectionRect.anchoredPosition.x + UGUIToolsWindow.interval, selectionRect.anchoredPosition.y);



        }
    }
}
